// noinspection JSAnnotator
let $;
var define = layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    $ = layui.$;
    let layer = layui.layer;
    let table = layui.table;
    let form = layui.form;
    let layDate = layui.laydate;
    let laypage = layui.laypage;
    zmnLayUtil.layFormVerify(form);
    let cols = [
        [
            // {field: 'useUserId', title: '用户ID', fixed: 'left', align: 'center'},
            {field: 'useId', title: '用户id', width: 200, align: 'center', templet:row=>{
                    return row.useId ? row.useId : "--" ;
                }},
            {field: 'batchName', title: '批次名称', align: 'center'},
            {
                field: 'preferentialValue', title: '面值', align: 'center', templet: function (row) {
                    var value = "";
                    if (row.preferentialValue) {
                        if (row.preferentialType && row.preferentialType == 1) {
                            value = row.preferentialValue / 10 + "折";
                        }
                        if (row.preferentialType && row.preferentialType == 2) {
                            value = (row.preferentialValue / 100) + "元";
                        }
                    }
                    return value;
                }
            },
            // {field: 'orderId', title: '订单号', align: 'center', templet:d=>{
            //     if (d.orderId) {
            //         return  d.orderId;
            //     } else {
            //         return "--";
            //     }
            //     }},
            {field: 'orderIdString', title: '关联订单信息', width: 200, align: 'center', templet:row=>{
                    return row.orderIdString ? row.orderIdString : "--" ;
             }},
            {
                field: 'activityType', title: '领券方式', align: 'center'
                , templet: function (row) {
                    //'活动类型，1：收藏有礼，2：分享有礼，3:折扣 ，4：立减',
                    var value = "";
                        if (row.preferentialType && row.preferentialType == 1) {
                            value = "折扣";
                        }
                        if (row.preferentialType && row.preferentialType == 2) {
                            value = "立减";
                        }
                        // if (row.preferentialType && row.preferentialType == 3) {
                        //     value = "折扣";
                        // }
                        // if (row.preferentialType && row.preferentialType == 4) {
                        //     value = "立减";
                        // }
                    return value;
                }
                // ,templet: function (row) {
                //     if (row.fromWay == 1){
                //         return '活动';
                //     }
                //     else if(row.fromWay == 2){
                //         return '会员';
                //     }
                // }
            },
            {
                field: 'status', title: '状态', align: 'center', templet: function (row) {
                    //状态：1-未使用，2-已发放，3-已使用，4-已失效
                    var value = "";
                    if (row.status && row.status == 2) {
                        value = "已发放";
                    }
                    if (row.status && row.status == 3) {
                        value = "已使用";
                    }
                    if (row.status && row.status == 4) {
                        value = "已失效(作废)";
                    }
                    if (row.status && row.status == 5) {
                        value = "已失效(过期失效)";
                    }
                    return value;
                }
            },
            {field: 'createTime', title: '发放时间', align: 'center', templet: function (row) {
                    return zmnUtil.createDate(row.createTime);
                }
                },
            {field: 'itemCode', title: '内部券码', align: 'center'},
            {
                field: 'issuer', title: '发放人', align: 'center', templet: function (row) {
                    var value = "";
                    if (row.issuer) {
                        value = row.issuer;
                    } else {
                        return "--";
                    }

                    // if (row.sourceType == 1) {
                    //     value = "人工";
                    // }
                    return value;
                }
            }
        ]
    ];


    form.render();

    var tableRender = function (data) {
        table.render({
            elem: "#zmn-coupquery-table",
            toolbar: "#zmn-coupquery-table-tool",
            title: "分类列表",
            where: data,
            data: json.items,
            limit: json.pageSize,
            cols: cols,
            page: true,
            height: 'full-120'
        });
    }();

    //分页加载数据，必须在表格初始化后
    laypage.render(zmnPage.laypageOptions(json));

    // //条件选择查询
    // form.on('submit(query-submit)', function (obj) {
    //     tableRender(obj.field);
    //     return false;
    // });
    //
    // let tableRender = function (data) {
    //     zmnLayUtil.layTableRender(table, cols, "zmn-coupquery-table", data, "/coup/coupquery/loadData.action", {
    //         limit: 50,
    //         page: true
    //     });
    // };
    // //加载数据
    // tableRender();

    //
    // //新增优惠
    // $('body').on('click', '#addCoupQueryBtn', function () {
    //     var url = '/coup/coupquery/add.action';
    //     var index = layer.open({
    //         title: '新增',
    //         type: 2,
    //         shift: 7,
    //         area: ['100%', '100%'],
    //         fix: true,
    //         maxmin: true,
    //         offset: ['6px'],
    //         content: url
    //     });
    //     layer.full(index);
    // // });


    function refresh(){
        tableRender();
    }
    $('body').on('click', '#addCoupQueryBtn', function () {
        var url = '/coup/coupquery/add.action';
       // var url = '/activity/config/coupinfo/add.action?activityId=' + json.currentActivityId;
        let data = {
            title: '',
            shade: 0.8,
            maxmin: false,
            resize: false,
            closeBtn: 1,
            end: refresh
        };
        zmnLayUtil.openIframeCustom(layer, url, data);
    });






    //对外暴露的接口
    exports('coupquery_list', {});
});
